﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nest;

namespace treeView.SqlDb.dbModel
{

    [Table("PLCInterface")]
    public class ToPLCInteraceNode
    {
        [Key]
        /// <summary>
        /// 序号
        /// </summary>
        public int Index { get; set; }
        /// <summary>
        /// 组别名字
        /// </summary>
        public string FileName { get; set; }
        /// <summary>
        /// 组别名字
        /// </summary>
        public string GroupName { get; set; }
        /// <summary>
        /// 节点名字
        /// </summary>
        public string NodeName { get; set; }
        /// <summary>
        /// 配置值
        /// </summary>
        public string NodePreSetValue { get; set; }
        /// <summary>
        /// 数据类型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 描述文本
        /// </summary>
        public string NodeDescription { get; set; }
        /// <summary>
        /// 写地址
        /// </summary>
        public string NodeWrite { get; set; }
        /// <summary>
        /// 读地址
        /// </summary>
        public string NodeRead { get; set; }

        /// <summary>
        /// 实时读取的值
        /// </summary>
        public string ActualRead
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(NodeRead))
                    {
                        var x = Machine.Instance.GetPlcByIndex(0).ReadData(NodeRead);
                        if (x == "")
                        {
                            return "ErrorConnect";
                        }
                        return x;
                    }
                    return "nullReadNode";
                }
                catch (Exception ex)
                {
                    return $"读取失败: {ex.Message}";
                }
            }
        }

    }
}
